package org.peterf.foxish.component.updater;

import org.peterf.foxish.component.property.Updatable;
import org.peterf.foxish.component.updater.interfaces.GradualReversable;

public class RotationUpdater implements GradualReversable {

    private float startRotation;
    
    private float endRotation;
    
    private float progress = 0.0f;
    
    private boolean reversed = false;
    
    public RotationUpdater(float startRotation, float endRotation) {
        this.startRotation = startRotation;
        this.endRotation = endRotation;
    }
    
    public float getProgress() {
        return progress;
    }

    public void setProgress(float progress) {
        this.progress = progress;
    }

    public boolean isFinished() {
        return progress >= 1.0f;
    }

    public void reset() {
        progress = 0.0f;
        reversed = false;
    }

    public void update(Updatable updatable, float elapsed) {
        if (reversed) {
            updatable.getMatrix().preRotation(startRotation * progress + (1.0f - progress) * endRotation);
        } else {
            updatable.getMatrix().preRotation(startRotation * (1.0f - progress) + progress * endRotation);
        }
    }

    public boolean isReversed() {
        return reversed;
    }

    public void reverse() {
        reversed = !reversed;
        progress = 0.0f;
    }

}
